﻿using IsPay.Logic;
using System;
using System.Collections.Generic;
using System.Data;

namespace IsPay.View
{
    public partial class EditMenu : System.Web.UI.Page
    {
        public IEnumerable<DataServer.Entities.Menu> lsmenus ;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USERID"] == null || Session["USERID"].ToString().Length <= 0)
            {
                Response.Write("<script>top.location='../Default.aspx'</script>");
            }
            if (Request.Params["ActionType"] != null)
            {
                String actionType = Request.Params["ActionType"].ToString();
                if (actionType.Equals("query"))
                {
                    String strqmenuid = Request.Params["txtmenuid"].ToString().Trim();
                    String strqmenuname = Request.Params["txtmenuname"].ToString().Trim();
                    MenuSer menuser = new MenuSer();
                    lsmenus = menuser.QueryMenus(strqmenuid, strqmenuname);
                }
                else if (actionType.Equals("modify"))
                {
                    String edittablejasn = Request.Params["updatejason"].ToString().Trim();
                    DataTable dtedit = CommonFormat.JasonToDataTable(edittablejasn);
                    if (dtedit != null && dtedit.Rows.Count > 0)
                    {
                        List<DataServer.Entities.Menu> lsadd = new List<DataServer.Entities.Menu>();
                        List<DataServer.Entities.Menu> lsmodify = new List<DataServer.Entities.Menu>();
                        List<DataServer.Entities.Menu> lsdel = new List<DataServer.Entities.Menu>();
                        DataRow[] dradds = dtedit.Select(" ADDFLAG=true AND DELETEFLAG=false ");
                        DataRow[] drmods = dtedit.Select(" ADDFLAG=false AND DELETEFLAG=false AND MODIFYFLAG=true");
                        DataRow[] drdels = dtedit.Select(" DELETEFLAG=true ");
                        lsadd = DataRowToMenu(dradds);
                        lsmodify = DataRowToMenu(drmods);
                        lsdel = DataRowToMenu(drdels);
                        MenuSer mser = new MenuSer();
                        String strmsg=mser.UpdateMenus(lsadd, lsmodify, lsdel,Session["USERID"].ToString());
                        if (strmsg.Equals("OK"))
                        {
                            lblMsg.Text = "更新成功";
                        }else
                        {
                            lblMsg.Text = strmsg;
                        }
                    }
                }
            }
        }
        private List<DataServer.Entities.Menu> DataRowToMenu(DataRow[] drs)
        {
            List<DataServer.Entities.Menu> lsmenu = new List<DataServer.Entities.Menu>();
            foreach(DataRow dr in drs)
            {
                DataServer.Entities.Menu menu = new DataServer.Entities.Menu();
                menu.MenuId = dr["MENUID"].ToString();
                menu.MenuName=dr["MENUNAME"].ToString();
                menu.HtmlName = dr["HTMLNAME"].ToString();
                lsmenu.Add(menu);
            }

            return lsmenu;
        }
    }
}